<?php
class Backend_SmiliesController extends Zend_Controller_Action
{
	private $registry; // registry
	private $auth; // user-values
	private $settings; // board-settings
	private $baseUrl; // helper
	
	public function init()
	{		
		// registry
		$this->registry = Zend_Registry::getInstance();
		
		// settings
		$this->settings	= $this->registry->get('settings');
		
		// helper
		$baseUrl		= new MHDEV_View_Helper_BaseUrl();
		$this->baseUrl	= $baseUrl->BaseUrl();
		
		// auth
		$auth = Zend_Auth::getInstance();
		$this->view->auth = $auth->hasIdentity();
    	
		if($auth->hasIdentity())
		{
			$this->view->auth_role 			= $auth->getIdentity()->role;
			$this->view->username 			= $auth->getIdentity()->username;
			$this->view->auth_uid			= $auth->getIdentity()->uid;
			$this->view->unread_messages	= $auth->getIdentity()->count_unread_messages;
		}
		else
		{
			$this->view->username 	= $this->registry->get('lang')->_('guest');
		}

		$this->auth = $auth;
    	
		// lang
		$this->view->translate = $this->registry->get('lang');
		
		// include js
		$this->view->headScript()->appendFile($this->baseUrl . '/static/js/jquery-1.3.2.min.js');
		$this->view->headScript()->appendFile($this->baseUrl . '/static/js/backend.js');
	}
	
	public function indexAction()
	{
		// helper
		$baseurl	= new MHDEV_View_Helper_BaseUrl();
		
		// db
		$smilies	= new Smilies();
		
		// query
		$getSmilies	= $smilies->fetchAll();
        
		// view
		$this->view->count		= $getSmilies->count();
		$this->view->results	= $getSmilies->toArray();
	}
	
/*
	 * Insert smilies in database
	 */
	public function addAction()
	{
		// vars
		$postData 	= $this->_request->getPost();
		
		// form
		$form 		= new SmiliesForm();
		
		// db
		$smilies	= new Smilies();
		
		// smilies
        $smiley_dir		= 'static/gfx/smilies/';
		if(is_dir($smiley_dir))
		{
			$smiley_handle 	= opendir($smiley_dir);
			$files 			= array();
			
			while($file = readdir($smiley_handle))
			{
				if(is_file($smiley_dir.$file))
				{
					$imgSize	= @getimagesize($smiley_dir.$file);
					if($imgSize['mime'] == 'image/gif' OR $imgSize['mime'] == 'image/png' OR $imgSize['mime'] == 'image/jpg')
					{
						$files[] 	= $file;
					}
				}
			}
			
			closedir($smiley_handle);
			natsort ($files);
			$this->view->smilies	= $files;
		}
		
		// get post		
		if ($this->_request->isPost() && $form->isValid($postData))
		{
			$form->removeElement('submit');
			
			$smilies->insert($form->getValues());
			
			$this->_redirect('/backend/smilies.html');
		}
		else
		{
			$form->populate($postData);
		}
		
		$this->view->form = $form;
	}
	
	
	
	/*
	 * Delete a smiley
	 */
	public function deleteAction()
	{
		// params
		$uid = $this->_request->getParam('uid', 0);
		
		// db
		$smilies = new Smilies();
		
		if($uid > 0)
		{
			$smilies->delete('uid=' . $uid);
			Zend_Registry::get('log')->info('[MODERATE] Smiley with ID ' . $uid . ' was deleted by Admin: ' . $this->auth->getIdentity()->uid);
			$this->_redirect('/backend/smilies.html');
		}
		
		$this->_redirect('/backend/smilies.html');
	}
}
?>